"""
@author: csy
@file: atm1.py
@time: 2023/12/4 16:34
"""
import json
user_data_file = "user_data.json"

def load_user_data():
    try:
        with open(user_data_file, "r") as file:
            user_data = json.load(file)
    except FileNotFoundError:
        user_data = {}
    return user_data

def save_user_data(user_data):
    with open(user_data_file, "w") as file:
        json.dump(user_data, file)

def login(username,password):
    user_data = load_user_data()
    if username in user_data and user_data[username]["password"] == password:
        return True
    else:
        print("用户名或密码错误")
        return False

def register(username,password):
    user_data = load_user_data()

    if username in user_data:
        print("用户名已存在，请选择其他用户名。")
    else:
        user_data[username] = {"password": password}
        save_user_data(user_data)
        print("注册成功！")


def deposit(username):
    user_data = load_user_data()
    user_data[username]["balance"] += amount
    save_user_data(user_data)
    print(f"存款成功！当前余额为：{user_data[username]['balance']}")

def withdraw(username):
    user_data = load_user_data()
    if amount > user_data[username]["balance"]:
        print("余额不足，无法取款。")
    else:
        user_data[username]["balance"] -= amount
        save_user_data(user_data)
        print(f"取款成功！当前余额为：{user_data[username]['balance']}")

def transfer(username):
    user_data = load_user_data()

    if recipient not in user_data:
        print("用户不存在，请检查用户名。")
        return

    if amount > user_data[username]["balance"]:
        print("余额不足，无法转账。")
    else:
        user_data[username]["balance"] -= amount
        user_data[recipient]["balance"] += amount
        save_user_data(user_data)
        print(f"转账成功！当前余额为：{user_data[username]['balance']}")

while True:
    print("\n1. 登录\n2. 注册\n3. 退出")
    choice = input("请选择操作 (1/2/3): ")


    if choice == "1":
        username = input("请输入用户名: ")
        password = input("请输入密码: ")
        if login(username,password):
            print("登录成功！")

            while True:
                print("\na. 存钱\nb. 取钱\nc. 转账\nd. 退出")
                operation = input("请选择操作 (a/b/c/d): ")

                if operation == "a":
                    amount = float(input("请输入存款金额: "))
                    deposit(username)
                elif operation == "b":
                    amount = float(input("请输入取款金额: "))
                    withdraw(username)
                elif operation == "c":
                    recipient = input("请输入对方用户名: ")
                    amount = float(input("请输入转账金额: "))
                    transfer(username)
                elif operation == "d":
                    break
                else:
                    print("无效的操作，请重新选择。")

    elif choice == "2":
        username = input("请输入用户名: ")
        password = input("请输入密码: ")
        initial_balance = float(input("请输入初始金额: "))
        register(username, password)

    elif choice == "3":
        print("感谢使用，再见！")
        break
    else:
        print("无效输入，请重新选择！")




